home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '90 / Source Code ƒ.sea / Source Code ƒ / Misc. C ƒ / Generic Drvr Installer f / Generic DRVR Inst.c next >
Encoding:
C/C++ Source or Header  |  1990-06-16  |  4.2 KB  |  185 lines  |  [TEXT/KAHL]

  1. /********************************************************************/
  2. /*    Generic DRVR Installer                                            */
  3. /*    Written by Shane D. Looker, Glamourware Software                */
  4. /*    With special bug fixes by Paul Campbell, SuperMac Technologies  */
  5. /*  Based upon a strategy supplied by David Shayer, Apple Computer  */
  6. /*                                                                    */
  7. /*    You need to add the ShowIcon code on your own.                    */
  8. /*                                                                    */
  9. /*    If you want real comments, add them!                            */
  10. /********************************************************************/
  11.  
  12. #define        NIL        0L
  13. #define        NODRIVERERROR    131
  14.  
  15. /*************************************************************************/
  16. /*    Define the name of the DRVR resource you want to load.  It must be a */
  17. /*    Pascal string which has the period to indicate that it is a driver.  */
  18. /*************************************************************************/
  19. #define        INIT    "\P.Ringworm"
  20.  
  21. #define        CalcResID(refNum, theID)    (-16384 + (32 * refNum) + theID)
  22.  
  23.  
  24. /* This is the structure for the extended launch parameters */
  25. main()
  26. {
  27.     short    err;
  28.     short    drRef, slotNum;
  29.     long    oldA4;
  30.     
  31.     /* THINK C sets up A4 for your globals (I believe.)  Just uncomment this */
  32.     /* don't complain that I didn't use a #ifdef...                          */
  33. /*    asm {
  34.             move.l A4, oldA4
  35.             move.l A0, A4
  36.         }
  37. */    
  38.     if (!isoptionkey())
  39.     {                
  40.     
  41.     
  42.         SetResLoad(TRUE);
  43.                     
  44.         slotNum = Find_Slot();
  45.         if (slotNum != 0)
  46.         {
  47.             drRef = Install_Driver(slotNum);
  48.                         
  49.             if (drRef == 0)
  50.             {
  51.                 return;
  52.             }
  53.         }
  54.     }
  55.     
  56.     
  57.     /* To fix A4 again if not in THINK C */
  58. /*    asm {
  59.             move.l oldA4, A4
  60.         }
  61. */
  62. }  /* End of MAIN program */
  63.  
  64.  
  65.  
  66.  
  67.  
  68. short Install_Driver(slot)
  69. short    slot;
  70. {
  71.  
  72.     Handle    Drvr, Data;
  73.     DCtlHandle    *Hands;
  74.     short    r;
  75.     short    oldDataID, newDataID;
  76.     char    name[255];
  77.     short    theID, oldID, newID;
  78.     ResType    TheType, oldType;
  79.     short    err, rnum;
  80.     short    theResAttrs;
  81.  
  82.         
  83.     
  84.     Drvr = Get1NamedResource('DRVR', INIT);
  85.     
  86.     GetResInfo(Drvr, &oldID, &oldType, &name[0]);
  87.     SetResInfo(Drvr, slot, &name[0]);
  88.     
  89.     oldID = CalcResID(oldID, 0);
  90.     newID = CalcResID(slot, 0);
  91.     Data = Get1Resource('DATA', oldID);
  92.     SetResInfo(Data, newID, NIL);
  93.  
  94.     err = OpenDriver(INIT, &rnum);        
  95.  
  96.     /****************************************************************************/
  97.     /* The following two lines of code are commented out right now, but you can */
  98.     /* re-install them to prevent the resources from being re-numbered          */
  99.     /****************************************************************************/
  100. /*
  101.     theResAttrs = GetResFileAttrs(CurResFile());
  102.     SetResFileAttrs(CurResFile(), theResAttrs && ~mapChanged);
  103. */
  104.     UpdateResFile(CurResFile());
  105.  
  106.     HLock(Drvr);
  107.     HLock(Data);
  108.     HNoPurge(Drvr);
  109.     HNoPurge(Data);
  110.     
  111.     DetachResource(Drvr);
  112.     DetachResource(Data);
  113.  
  114.     /* You could issue an Control statement at this point if you wanted to */
  115. /*
  116.     err = Control(rnum, accRun, NIL);
  117.     
  118.     if (err != 0)
  119.         return(0);
  120. */
  121.     
  122.     return(rnum);
  123. }
  124.  
  125.  
  126. short Find_Slot()
  127. {
  128.     DCtlHandle    Hands = (DCtlHandle) -1;
  129.     short        slot;
  130.  
  131.     
  132.     /* Paul Campbell says, "Don't go below Hex 28 (I Think.)"    */
  133.     for(slot = UnitNtryCnt-1; (slot > (short)0x28) && (Hands != NIL); slot--)
  134.         Hands = GetDCtlEntry(-(slot-1));
  135.     
  136.     if (slot == (short) 0x28)        /* OOPS!  We Didn't Find An Open Slot! */
  137.         slot = 0;                    /* you should really extend the unit table here ....*/
  138.                                     /* newptr a new one, zero it, blockmove the old one, */
  139.                                     /* to the new one, update UnitNtryCnt and UTableBase */
  140.                                     /* But remember to turn off all interrupts first */
  141.     /* We should now have an empty slot number or 0 */
  142.         
  143.     return(slot);
  144. }
  145.  
  146.  
  147. isoptionkey()
  148. {
  149.     return(iskeydown(58));
  150. };
  151.  
  152.  
  153. /*     What is THIS!  Looks like an Intel map...
  154.     The map looks like :
  155.     
  156.             87654321 FEDCBA9
  157.     for each byte....  HUMMMPH   */
  158.     
  159.     
  160. iskeydown(keycode)
  161. int    keycode;
  162. {
  163.     /* returns true if the key specified in the map by
  164.         keycode is down false otherwise */
  165.         
  166.     KeyMap    thekeys;
  167.     long    mask;
  168.     int        bank;
  169.     int        temp;
  170.         
  171.     GetKeys(&thekeys);    /* check what keys are down -- Option is 58 */
  172.     
  173.     bank = keycode / 32;        /* get the right bank to use */
  174.     temp = (keycode % 32);         /* ok now get the byte .... */
  175.     
  176.     temp = ((temp / 8 ) * 8 ) + ( 7 - (temp % 8));
  177.     
  178.     mask = 1L << ( 31 - temp);
  179.     if (( thekeys.Key[bank] & mask ) == 0 )
  180.         return(0);
  181.     else
  182.         return(1);
  183. };
  184.  
  185.